package springTaskExecutor.example;

import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Component;

/**
 * @author CKC
 * @DateTime 2021/12/12 17:09
 */
@Component
public class TaskExecutorExample {
    private final TaskExecutor taskExecutor;

    public TaskExecutorExample(TaskExecutor taskExecutor) {
        this.taskExecutor = taskExecutor;
    }

    public void printMessages() {
        for (int i = 0; i < 10; i++) {
            taskExecutor.execute(new MessagePrinterTask("Message" + i));
        }
    }

    private class MessagePrinterTask implements Runnable {

        private final String message;

        public MessagePrinterTask(String message) {
            this.message = message;
        }

        public void run() {
            System.out.println(Thread.currentThread().getName() + "\t" + message);
        }
    }
}
